package com.moyu.admin.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ArrayUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.moyu.admin.bean.BaseReq;
import com.moyu.admin.bean.BaseResp;
import com.moyu.admin.service.ISysRoleService;
import com.moyu.data.domain.SysRole;
import com.moyu.data.domain.UserInfo;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.ArrayUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/admin/role")
@Slf4j
public class RoleController {
  @Autowired private ISysRoleService roleService;

  @RequestMapping("/index")
  public String index() {
    return "/admin/role/index";
  }

  @RequestMapping("/list")
  @ResponseBody
  public Map<String, Object> list(BaseReq req) {
    IPage<SysRole> page = roleService.page(new Page<>(req.getPage(), req.getRows()), null);
    Map<String, Object> resultMap = new HashMap();
    resultMap.put("rows", page.getRecords());
    resultMap.put("total", page.getTotal());
    return resultMap;
  }

  @PostMapping("/add")
  @ResponseBody
  public BaseResp<UserInfo> add(SysRole role) {
    role.setAvailable(true);
    roleService.save(role);
    return BaseResp.success();
  }

  @PostMapping("/edit")
  @ResponseBody
  public BaseResp<UserInfo> edit(SysRole role) {
    roleService.updateById(role);
    return BaseResp.success();
  }

  @PostMapping("/del")
  @ResponseBody
  public BaseResp<UserInfo> del(Long id) {
    roleService.removeById(id);
    return BaseResp.success();
  }
  @PostMapping("/grant")
  @ResponseBody
  public BaseResp<UserInfo> grant(@RequestParam Long id, @RequestParam Long[] nodeId) {
    roleService.grantPermission(id, nodeId);
    return BaseResp.success();
  }
}
